package com.wbdgj.base;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.model.NetFreeBranchModel;
import com.wbdgj.model.UserModel;
import com.wbdgj.ui.DemoIntentService;
import com.wbdgj.ui.DemoPushService;
import com.wbdgj.ui.PushActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.cache.SPUtil;
import com.uuzuche.lib_zxing.ZApplication;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.PlatformConfig;


/**
 * Created by liub on 2017/6/26 13:52.
 */

public class BaseApplication extends ZApplication {
    private static BaseApplication instance;
    private SPUtil spUtil;
    private UserModel userModel;
    private LinkedTreeMap LinkedTreeMap;
    private List<LinkedTreeMap> linkedTreeMapList;
    private static DemoHandler handler;
    public Vibrator mVibrator;
    /**
     * 应用未启动, 个推 service已经被唤醒,保存在该时间段内离线消息(此时 GetuiSdkDemoActivity.tLogView == null)
     */
    public static StringBuilder payloadData = new StringBuilder();
    private static PushActivity pushActivity;

    private HashMap<String, NetFreeBranchModel> netFreeBranchModelHashMap;

    public static String ImagePath;
    public static String ImagePath2;
    public static String ImagePath3;
    public static String VideoPath;
    @Override
    public void onCreate() {
        super.onCreate();
        MultiDex.install(this);
        instance = this;
        spUtil = new SPUtil(this);
        PushManager.getInstance().initialize(this,  DemoPushService.class);
        PushManager.getInstance().registerPushIntentService(this, DemoIntentService.class);
        if (handler == null) {
            handler = new DemoHandler();
        }
        JShareInterface.setDebugMode(true);
        PlatformConfig platformConfig = new PlatformConfig()
                .setWechat("wxf1069c4f961f956e", "2c0c2f2e507f3b84ef5b33dcd1fb6dd2")//全城VIP
//                .setWechat("wx1f834fc98c08e756", "9095f4608a89db392cc3defc223fe15d")//风客连锁网咖

                .setQQ("101529516", "896f4b28cc9e25c18572d0b5d9f7e983")//全城VIP
//                .setQQ("101553122", "d07e9806671113cdd221505648c58168")//风客连锁网咖
                .setSinaWeibo("374535501", "baccd12c166f1df96736b51ffbf600a2", "https://www.jiguang.cn")
                .setFacebook("1847959632183996", "JShareDemo")
                .setTwitter("eRJyErWUhRZVqBzADAbUnNWx5", "Oo7DJMiBwBHGFWglFrML1ULZCUDlH990RlJlQDdfepm3lToiMC");
        JShareInterface.init(this, platformConfig);

        new Thread(){
            @Override
            public void run() {
                File imageFile =  copyResurces( "jiguang_test_img.jpg", "jiguang_test_img.jpg", 0);
                File imageFile2 =  copyResurces( "ic_launcher.png", "ic_launcher.png", 0);
                File imageFile3 =  copyResurces( "yqhy_bg.png", "yqhy_bg.png", 0);
                File videoFile = copyResurces( "jiguang_test.mp4", "jiguang_test.mp4", 0);
                if(imageFile != null){
                    ImagePath = imageFile.getAbsolutePath();
                }
                if(imageFile2 != null){
                    ImagePath2 = imageFile2.getAbsolutePath();
                }
                if(imageFile3 != null){
                    ImagePath3 = imageFile3.getAbsolutePath();
                }

                if(videoFile != null){
                    VideoPath = videoFile.getAbsolutePath();
                }

                super.run();
            }
        }.start();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

    private  File copyResurces(String src, String dest, int flag){
        File filesDir = null;
        try {
            if(flag == 0) {//copy to sdcard
                filesDir = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/jiguang1/" + dest);
                File parentDir = filesDir.getParentFile();
                if(!parentDir.exists()){
                    parentDir.mkdirs();
                }
            }else{//copy to data
                filesDir = new File(getFilesDir(), dest);
            }
            if(!filesDir.exists()) {
                filesDir.createNewFile();
                InputStream open = getAssets().open(src);
                FileOutputStream fileOutputStream = new FileOutputStream(filesDir);
                byte[] buffer = new byte[4 * 1024];
                int len = 0;
                while ((len = open.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, len);
                }
                open.close();
                fileOutputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            if(flag == 0){
                filesDir = copyResurces( src,  dest, 1);
            }
        }
        return filesDir;
    }

    public SPUtil getSpUtil() {
        if (spUtil == null)
            spUtil = new SPUtil(this);
        return spUtil;
    }
    public boolean isFirstEnter() {
        return getSpUtil().getBoolean(SpKeyUtils.FIRST_ENTER, true);
    }

    public void setNotFirstEnter() {
        getSpUtil().putBoolean(SpKeyUtils.FIRST_ENTER, false);
    }

    public boolean isGGFirstEnter() {
        return getSpUtil().getBoolean(SpKeyUtils.GG_ENTER, true);
    }

    public void setGGFirstEnter() {
        getSpUtil().putBoolean(SpKeyUtils.GG_ENTER, false);
    }

    public static BaseApplication getInstance() {
        return instance;
    }

    public void setUserModel(UserModel userModel) {
        this.userModel = userModel;
        getSpUtil().putObject(SpKeyUtils.USER, userModel);
    }

    public UserModel getUserModel() {
        if (userModel == null)
            userModel = (UserModel) getSpUtil().getObject(SpKeyUtils.USER, UserModel.class);
        return userModel;
    }
    public boolean isLogin() {
        return getSpUtil().getString(SpKeyUtils.IS_AUTO_LOGIN).equals("1");
    }

    public boolean checkLogin(BaseActivity context) {
        if (!isLogin()) {
//            context.launch(LoginActivity.class);
            return false;
        } else {
            return true;
        }
    }



    public LinkedTreeMap getLinkedTreeMap() {
        return LinkedTreeMap;
    }
    public void setLinkedTreeMap(LinkedTreeMap LinkedTreeMap) {
        this.LinkedTreeMap = LinkedTreeMap;
    }

    public List<LinkedTreeMap> getlinkedTreeMapList() {
        return linkedTreeMapList;
    }
    public void setlinkedTreeMapList(List<LinkedTreeMap> linkedTreeMapList) {
        this.linkedTreeMapList = linkedTreeMapList;
    }

    public HashMap<String, NetFreeBranchModel> getNetFreeBranchModelHashMap() {
        return netFreeBranchModelHashMap;
    }
    public void setNetFreeBranchModelHashMap(HashMap<String, NetFreeBranchModel> netFreeBranchModeHashMap) {
        netFreeBranchModelHashMap = new HashMap<String, NetFreeBranchModel>();
        for(Iterator it = netFreeBranchModeHashMap.keySet().iterator(); it.hasNext();){
            String key = it.next().toString();
            this.netFreeBranchModelHashMap.put(key, netFreeBranchModeHashMap.get(key));
        }
    }

    public static void sendMessage(Message msg) {
        handler.sendMessage(msg);
    }

    public static class DemoHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (pushActivity != null) {
                        payloadData.append((String) msg.obj);
                        payloadData.append("\n");
                    }
                    break;

                case 1:
                    if (pushActivity != null) {
                    }
                    break;
            }
        }
    }

}
